//
//  YSGChatTimerView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/4/13.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit

class YSGChatTimerView: UIView {

    @IBOutlet private weak var timeLabel: UILabel!
    
    /// 毫秒时间戳
    var timeStamp: Int = 0
    private var timer: YSGTimer!
    
    @IBOutlet weak var titleLabel: UILabel!
    deinit { timer.invalidate() }
    
    func configure(with timeStamp: Int,status:chatStatusEnum) {
        self.timeStamp = Int(timeStamp / 1000)
        switch status {
        case .nurse:
            titleLabel.text =  "护理咨询中"
        case .advisory:
            titleLabel.text =  "图文咨询中"
        }
        if self.timeStamp == 0 {
            isHidden = true
            return
        }
        timeLabel.text = Date.timeStampToString(timeStamp: self.timeStamp)
        setupTimer()
    }
    
    private func setupTimer() {
        timer = YSGTimer.scheduledTimer(with: 1, repeats: true, block: { [weak self] _ in
            self?.timerRun()
        })
        timer.fire()
    }
    
    private func timerRun() {
        timeStamp -= 1
        if timeStamp <= 0 {
            isHidden = true
            timer.invalidate()
        }
        timeLabel.text = Date.timeStampToString(timeStamp: timeStamp)
    }
}
